package com.zhangck.bos_fore.action;


import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.poi.ss.formula.functions.Code;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.zhangck.bos_fore.uitls.MailUtils;
import com.zhangck.bos_fore.uitls.SmsDemo;

import cn.itcast.crm.domain.Customer;

/** 
* @author 上午茶
* @version 创建时间：2018年6月4日 下午7:23:46 
* 类说明 
*/@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
@Service
public class RegisterAction extends BaseAction<Customer> {
	
	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;

	@Action(value="Custmer_sendcoed",results={@Result(name="aaa", type="redirect", location="/signup.html")})
	public String Custmer_sendcoed() throws ClientException{
			String code = RandomStringUtils.randomNumeric(4);
			ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(),code);
			System.out.println("生成的验证码是："+code);
			
			final String msg ="aaaaaaaaaaaaaaaaa:  "+code ;
			
			jmsTemplate.send("bos_sms", new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
						MapMessage mapMessage = session.createMapMessage();
						mapMessage.setString("telephone", model.getTelephone());
						mapMessage.setString("msg", msg);
					return mapMessage;
				}
			});
			
		//	SendSmsResponse response = SmsDemo.sendSms(model.getTelephone(),code);
			if ("OK".equals("OK")) {
				System.out.println("发送成功");
				return NONE;
			}
			return "aaa";
	}
	
	private String code;
	
	public void setCode(String code) {
		this.code = code;
	}

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Action(value="CoustermRegist",results={@Result(name="success", type="redirect", location="/signup-success.html"),
			@Result(name="aaa", type="redirect", location="/signup.html")})
	public String CoustermRegist() {
		String code1 = (String) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());
		if (code1==null||!code1.equals(code)) {
			System.out.println("验证码错误");
			return "aaa";
		}
		Response post = WebClient.create("http://127.0.0.1:8088/services/customerService/CustomerEmail/loginCutomer").accept(MediaType.APPLICATION_JSON_TYPE).post(model);
		post.getStatus();
		if (post.getStatus()!=204) {
			System.out.println("后台出现异常");
			return "aaa";
		}
		
		String codeID = RandomStringUtils.randomNumeric(10);//激活码
		redisTemplate.opsForValue().set(model.getTelephone(), codeID, 24, TimeUnit.HOURS);//吧激活和电话号码绑定存进redis里
		String conecxt  = "<h1>来自速运快递天堂上午茶官方快递的激活邮件:请点击下面链接激活!</h1><h3><a href='http://localhost:8090/customer_activeMail?telephone="+model.getTelephone()+"&codeID="+codeID+"'>上午茶速运快递邮箱绑定地址</a></h3>" 
				;
		MailUtils.sendMail("上午茶速运快递", conecxt, model.getEmail());
		System.out.println("客户端注册成功");
		return "success";
	}
	
	private String codeID;
	
	public void setCodeID(String codeID) {
		this.codeID = codeID;
	}


	@Action(value="customer_activeMail")
	public String customer_activeMail() {
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		String redisCode = redisTemplate.opsForValue().get(model.getTelephone());
		if (redisCode==null||!redisCode.equals(codeID)) {
			try {
				ServletActionContext.getResponse().getWriter().write("激活码无效,请重新登录，重新绑定邮箱");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			Customer customer = WebClient.create("http://127.0.0.1:8088/services/customerService/CustomerEmail/getCutomer/"+model.getTelephone()).
			accept(MediaType.APPLICATION_JSON).
			get(Customer.class);
			
			if (customer.getType()==null||customer.getType()!=1) {
				WebClient.create("http://127.0.0.1:8088/services/customerService/CustomerEmail/UpdateCutomer/"+model.getTelephone()).
					accept(MediaType.APPLICATION_JSON).
					get();
				try {
					ServletActionContext.getResponse().getWriter().write("恭喜你，绑定成功");
				} catch (IOException e) {
					e.printStackTrace();
			}
		}else {
			try {
				ServletActionContext.getResponse().getWriter().write("你已绑定，无需重复绑定");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
			redisTemplate.delete(model.getTelephone());
		}
		return code;
	}
	
}
